<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">

      <el-form-item label="商家名称" prop="name">
        <el-input
v-model="queryParams.name" placeholder="请输入商家名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input
v-model="queryParams.mobile" placeholder="请输入联系电话" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>


      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" class="!w-240px" clearable placeholder="请选择状态">
          <el-option
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalName">
        <el-input
v-model="queryParams.legalName" placeholder="请输入法人姓名" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>



      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['merchant:info:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['merchant:info:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="用户ID" align="center" prop="id" />
      <el-table-column label="logo" align="center" prop="logo" width="70px">
        <template #default="scope">
          <el-avatar :src="scope.row.logo" />
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" min-width="80" />
      <el-table-column label="联系电话" align="center" prop="mobile" min-width="80" />
      <el-table-column label="经营品类" align="center" prop="categoryId" icon="ep:apple" min-width="80">
        <template #default="row">
          {{ formatCategoryName(row.categoryId) }}
        </template>
      </el-table-column>
      <el-table-column label="所在地" icon="ep:location" min-width="80">
        <template #default="scope">
          {{ scope.row.areaName }}
        </template>
      </el-table-column>
      <el-table-column label="身份类型" align="center" prop="identityType" min-width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MERCHANT_IDENTITY_TYPE" :value="scope.row.identityType" />
        </template>
      </el-table-column>

      <el-table-column label="发货方式" align="center" prop="deliveryType" min-width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MERCHANT_DELIVERY_TYPE" :value="scope.row.deliveryType" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" min-width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.AUDIT_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="法人姓名" align="center" prop="legalName" min-width="80"/>
      <el-table-column label="法人身份证" align="center" prop="legalIdCardNo" width="150" />
      <el-table-column label="法人身份证失效日期" align="center" prop="legalIdCardExpireDate" :formatter="dateFormatter2" min-width="200" />
      <el-table-column label="社会信用代码" align="center" prop="creditCode" />
      <el-table-column label="社会信用代码失效日期" align="center" prop="creditCodeExpireDate" :formatter="dateFormatter2"  min-width="200"/>

      <el-table-column label="操作" align="center" width="200px" fixed="right"  min-width="200">
        <template #default="scope">
          <el-button
v-if="scope.row.status === -1" link type="primary" @click="openDetailForm(scope.row.id)"
            v-hasPermi="['merchant:info:update']">
            审核
          </el-button>
          <el-button
v-else link type="primary" @click="openDetailForm(scope.row.id)"
            v-hasPermi="['merchant:info:update']">
            查看
          </el-button>
          <el-button
link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['merchant:info:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['merchant:info:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MerchantForm ref="formRef" @success="getList" />

  <MerchantDetail ref="detailFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { MerchantApi, MerchantVO } from '@/api/mall/product/merchant';
import MerchantForm from './MerchantForm.vue';
import MerchantDetail from './MerchantDetail.vue';
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict';
import { handleTree, treeToString } from '@/utils/tree'
import * as ProductCategoryApi from '@/api/mall/product/category'

/** 商家信息 列表 */
defineOptions({ name: 'MerchantInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<MerchantVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  username: undefined,
  password: undefined,
  logo: undefined,
  name: undefined,
  descrption: undefined,
  categoryId: undefined,
  type: undefined,
  deliveryType: undefined,
  status: undefined,
  legalName: undefined,
  legalPhone: undefined,
  legalIdCardNo: undefined,
  legalIdCardValidity: undefined,
  businessScope: undefined,
  creditCode: undefined,
  creditCodeValidity: undefined,
  address: undefined,
  mobile: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MerchantApi.getInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const categoryList = ref() // 分类树
const formatCategoryName = (categoryId: number) => {
  return treeToString(categoryList.value, categoryId)
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()

const detailFormRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const openDetailForm = (id?: number) => {
  detailFormRef.value.open(id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MerchantApi.deleteInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MerchantApi.exportInfo(queryParams)
    download.excel(data, '商家信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  const data = await ProductCategoryApi.getCategoryList({})
  categoryList.value = handleTree(data, 'id', 'parentId')
})
</script>